package net.ermannofranco.xml;

/**
 * Superclass of all tags, it serves also as generic comment node.
 * 
 * @author ermanno
 * 
 */
public class CommentNode implements IndentNode {

	private static final long serialVersionUID = 2626979663619944527L;
	private final String content;
	protected final int level;
	// default indenta nella stampa e non nel file
	protected Indent printIndent = Indent.MEDIUM;
	protected Indent saveIndent = Indent.NO;
	//default separa i tag al primo livello
	protected BlankLine blankLine = BlankLine.FIRST_LEVEL;

	protected CommentNode(String content, int level) {
		this.content = Static.toXmlString(content);
		this.level = level;
	}

	public String toPrint() {
		StringBuilder sb = new StringBuilder();
		switch (blankLine) {
		case FIRST_LEVEL:
			if (level == 1)
				sb.append(Static.CRLF);
			break;
		case ALL_LEVELS:
			sb.append(Static.CRLF);
			break;
		default:
			break;
		}
		Static.indent(sb, printIndent, level);
		sb.append(Static.OPEN_COMMENT).append(content).append(
				Static.CLOSE_COMMENT);
		return sb.toString();

	}

	public String toSave() {
		StringBuilder sb = new StringBuilder();
		Static.indent(sb, saveIndent, level);
		sb.append(Static.OPEN_COMMENT).append(content).append(
				Static.CLOSE_COMMENT);
		return sb.toString();
	}

	@Override
	public String toString() {
		return toPrint();
	}

	public void setPrintIndent(Indent indent) {
		this.printIndent = indent;
	}

	public void setSaveIndent(Indent indent) {
		this.saveIndent = indent;
	}

	protected void setBlankLine(BlankLine blankLine) {
		this.blankLine = blankLine;

	}
}
